package solutions;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-28
 * Time: 15:18
 */

//https://leetcode.cn/problems/rotate-array/submissions/397502587/?envType=study-plan&id=suan-fa-ru-men&plan=algorithms&plan_progress=bmyvagt
class Solution8 {
    public void Reverse (int[] nums, int start, int end){
        while (start < end){
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }
    public void rotate(int[] nums, int k) {
        //防止k比数组的长度长，造成数组越界
        k %= nums.length;
        //先将全部数组逆置
        Reverse(nums, 0, nums.length - 1);
        //再逆置[0, k - 1]
        Reverse(nums, 0, k - 1);
        //最后逆置[k, nums.length - 1]
        Reverse(nums, k, nums.length - 1);
    }
}
